#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>

#include "DomTree.h"
using namespace std;

int main( int argc, char *argv[] )
{
	if ( argc < 2 )
	{
		printf( "Useage: ./a.out <url>\n" );
		return 0;
	}
	
	char urlDownload[1024] = { 0 };
		
	sprintf( urlDownload, "wget -O temp.html %s", argv[1] );
	
//	system( urlDownload );
	
	/*
	string strHtml;
	ifstream fileHtml( "temp.html" );
	
	while ( !fileHtml.eof() )
	{
		string temp;
		fileHtml >> temp;
		strHtml += temp;
		strHtml += "\n";
	}
	//fileHtml >> buf;
	//fileHtml.write( buf, 65536 );

	cout << "strHtml++++++++++++++++++++++++++++++" << endl;
	cout << strHtml << endl;
	*/
	FILE *fp = fopen( "temp.html", "rb");
	char buf[65536] = { 0 };
	fread( buf, sizeof(char), 65536, fp );
//	cout << "buf++++++++++++++++++++++++++++++" << endl;
//	cout << buf << endl;
	
	CDomTree tree;
	tree.LoadTree( buf );
	
//	cout << "tree.buf=[" << tree.getBuff() << "]" << endl;
	
	list<CNode> listNodes = tree.GetTagByClass( "c-title" );
	cout << "list size = [" << listNodes.size() << "]" << endl;
	
	//CNode node = listNodes.get(listNodes.begin());
	if ( listNodes.empty() )
		return 0;
	
//	CNode node = (CNode)(*listNodes.begin());
//	cout << "##############node inner html [" << node.GetInnerHtml() << "]" << endl;
//	cout << "##############node inner text [" << node.GetInnerText() << "]" << endl;

	for ( list<CNode>::iterator it = listNodes.begin(); it != listNodes.end(); ++it )
	{
		cout << "it text[" << it->GetInnerText() << "]" << endl;
		
	}
	
	return 0;
}